//
//  PZTipsSureAlertView.m
//  biyingqiquan
//
//  Created by LK-IOS on 2018/5/9.
//  Copyright © 2018年 LK-BYQQIOS. All rights reserved.
//

#import "PZTipsSureAlertView.h"

@interface PZTipsSureAlertView()

{
    CGSize  _contentSize;
    CGFloat _paddingTop, _paddingBottom, _paddingLeft; // paddingRight = paddingLeft
    CGFloat _spacing;
}
/**
 标题
 */
@property (nonatomic , strong) UILabel *titleLabel;


/**
 标题下划线
 */
@property (nonatomic , strong) UILabel *line;

/**
 内容
 */
@property (nonatomic , strong) UILabel *contentLabel;

/**
 选择按钮
 */
@property (nonatomic , strong) SYNoHeightedButton *selectedButton;
/**
 选择按钮说明
 */
@property (nonatomic , strong) UILabel *selectedTipLabel;
/**
 确认按钮
 */
@property (nonatomic , strong) SYNoHeightedButton *okeyButton;
/**
 须知label
 */
@property (nonatomic , strong) UILabel *tipsLabel;

/**
 说明label
 */
@property (nonatomic , strong) UILabel *explainLabel;



@property (nonatomic, copy) void (^selectedHandler)(SYNoHeightedButton *selectedHandler);


@property (nonatomic, copy) void (^okeyHandler)(SYNoHeightedButton *okeyHandler);


@property (nonatomic, copy) void (^explainHandle)(void);

@end

@implementation PZTipsSureAlertView

-(instancetype)initWithConstantWidth:(CGFloat)constantWidth Title:(NSString *)title message:(NSString *)message SelectedTip:(NSString *)selectedTip ButtonTitle:(NSString *)buttonTitle Explain:(NSString *)explain SelectedHandler:(void (^)(SYNoHeightedButton *))selectedHandler OkeyHandler:(void (^)(SYNoHeightedButton *))okeyHandler ExplainHandle:(void (^)(void))explainHandle
{

    if (self = [super init]) {
     
        self.selectedHandler = selectedHandler;
        self.okeyHandler = okeyHandler;
        
        self.explainHandle = explainHandle;
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.clipsToBounds = NO;
        
        //默认属性值
        self.buttonHeight = 42.f;
        _contentSize.width = 200; // default width = 200
        if (constantWidth > 0) _contentSize.width = constantWidth;
        _paddingTop = 15;
        _paddingBottom = 15;
        _paddingLeft = 20;
        _spacing = 15;
        
        
        if (title.length) {//标题
            _titleLabel = [[UILabel alloc] init];
            _titleLabel.text = title;
            _titleLabel.numberOfLines = 0;
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.font = [UIFont systemFontOfSize:20];
            [self addSubview:_titleLabel];
            
            _titleLabel.size = [_titleLabel sizeThatFits:CGSizeMake(_contentSize.width-2*_paddingLeft, MAXFLOAT)];
            _titleLabel.y = _paddingTop;
            _titleLabel.centerX = _contentSize.width / 2;
            _contentSize.height = _titleLabel.bottom;
            
            //标题下划线
            _line = [UILabel new];
            _line.backgroundColor = NumberColor(236, 236, 236);
            [self addSubview:_line];
            _line.frame = CGRectMake(rate375(22),_titleLabel.bottom +_paddingBottom, (_contentSize.width - 2*rate375(22)), 1);
            _contentSize.height = _line.bottom;
        }
        
        
        if (message.length) {
            _contentLabel = [[UILabel alloc] init];
            _contentLabel.numberOfLines = 0;
            _contentLabel.font = [UIFont systemFontOfSize:16];
            _contentLabel.textColor = [UIColor colorWithHexString:battleshipGrey];
            
            [self addSubview:_contentLabel];
            
            NSAttributedString *string = [NSString createAttriStringWithString:message andSpace:2.0 Font:[UIFont systemFontOfSize:16]];
            _contentLabel.attributedText = string;

            CGFloat Height =   [NSString getHeightWithText:message font:[UIFont systemFontOfSize:16] MaxWidth:_contentSize.width - 2*_paddingLeft lineSpace:2];
            
            
            if (Height < 22) {
                //一行文字居中显示
                _contentLabel.textAlignment = NSTextAlignmentCenter;
            }else
            {
                //多行左对齐
                _contentLabel.textAlignment = NSTextAlignmentLeft;
            }
            
            if (Height < 64) {
                //内容自适应高度 默认最低60
                Height = 64;
            }
            
            
            _contentLabel.size = CGSizeMake(_contentSize.width - 2*_paddingLeft, Height);
            _contentLabel.y = _line.bottom + _spacing;
            _contentLabel.centerX = _contentSize.width / 2;
            
            _contentSize.height = _contentLabel.bottom;
        }
        
        
        //选择按钮
        _selectedButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        _selectedButton.backgroundColor = [UIColor clearColor];
        [_selectedButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchDown];
        [_selectedButton setBackgroundImage:[UIImage imageNamed:@"tips_nor"] forState:UIControlStateNormal];
        [_selectedButton setBackgroundImage:[UIImage imageNamed:@"tips_selected"] forState:UIControlStateSelected];
        [self addSubview:_selectedButton];

        
        _selectedButton.selected = [SYUserDataManger sharedInstance].getUserTips;
        
        _selectedButton.x = _paddingLeft;
        _selectedButton.y = _contentLabel.bottom + _spacing;
        _selectedButton.size = CGSizeMake(20, 20);
        
        _contentSize.height = _selectedButton.bottom;
        
        //选择按钮说明
        if (selectedTip.length) {
            _selectedTipLabel = [[UILabel alloc] init];
            _selectedTipLabel.text = selectedTip;
            _selectedTipLabel.numberOfLines = 0;
            _selectedTipLabel.textAlignment = NSTextAlignmentLeft;
            _selectedTipLabel.font = [UIFont systemFontOfSize:16];
            _selectedTipLabel.textColor = [UIColor colorWithHexString:battleshipGrey];
            [self addSubview:_selectedTipLabel];
            
            _selectedTipLabel.size = [_selectedTipLabel sizeThatFits:CGSizeMake(_contentSize.width-2*_paddingLeft, MAXFLOAT)];
            _selectedTipLabel.centerY = _selectedButton.centerY;
            _selectedTipLabel.x = CGRectGetMaxX(_selectedButton.frame) + 5.f;
            _contentSize.height = _selectedTipLabel.bottom;
        }
        
        
        //确认按钮
        _okeyButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        _okeyButton.backgroundColor = [UIColor colorWithHexString:Orange];
        _okeyButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_okeyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_okeyButton setTitle:buttonTitle forState:UIControlStateNormal];
        [_okeyButton addTarget:self action:@selector(okeyButtonClick:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:_okeyButton];
        _okeyButton.layer.cornerRadius = 4.f;
        _okeyButton.layer.masksToBounds = YES;
        CGFloat width = _contentSize.width - _paddingLeft * 2;
        _okeyButton.size = CGSizeMake(width, self.buttonHeight);
        _okeyButton.y = _selectedTipLabel.bottom + _spacing;
        _okeyButton.centerX = _contentSize.width / 2;
        _contentSize.height = _okeyButton.bottom;
        
        
        //协议
        if (explain.length) {
            _explainLabel = [[UILabel alloc] init];
            _explainLabel.numberOfLines = 0;
            _explainLabel.textAlignment = NSTextAlignmentCenter;
            _explainLabel.font = [UIFont systemFontOfSize:14];
            [self addSubview:_explainLabel];
            
            NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:explain];
            
            [attStr addAttribute:NSUnderlineStyleAttributeName
                           value:@(NSUnderlineStyleSingle)
                           range:NSMakeRange(0, explain.length)];
            
            [attStr addAttribute:NSUnderlineColorAttributeName
                           value:[UIColor colorWithHexString:paleRed]
                           range:NSMakeRange(0, explain.length)];
            //NSForegroundColorAttributeName
            [attStr addAttribute:NSForegroundColorAttributeName
                           value:[UIColor colorWithHexString:paleRed]
                           range:NSMakeRange(0, explain.length)];
            
            _explainLabel.attributedText = attStr;
            
            _explainLabel.size = [_explainLabel sizeThatFits:CGSizeMake(_contentSize.width-2*_paddingLeft, MAXFLOAT)];
            _explainLabel.y = _okeyButton.bottom + _paddingBottom;
            _explainLabel.centerX = _contentSize.width / 2;
            
            LK(weakSelf)
            [_explainLabel yb_addAttributeTapActionWithStrings:@[explain] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
                if ([string isEqualToString:explain]) {
                    if (weakSelf.explainHandle) {
                        weakSelf.explainHandle();
                    }
                }
            }];
            //是否设置点击效果
            _explainLabel.enabledTapEffect = YES;

            _contentSize.height = _explainLabel.bottom;
        }
        
        
        
        self.size = CGSizeMake(_contentSize.width, _contentSize.height + _paddingBottom);
        if (!title.length && !message.length) {
            self.size = CGSizeZero;
        }
        

        
    }
    return self;
}

-(void)selectedButtonClick:(SYNoHeightedButton *)button
{
    _selectedButton.selected = !button.selected;
    if (self.selectedHandler) {
        self.selectedHandler(button);
    }
}


-(void)okeyButtonClick:(SYNoHeightedButton *)button
{
    if (self.okeyHandler) {
        self.okeyHandler(button);
    }
}

@end
